package steed.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import steed.domain.system.Config;
import steed.ext.domain.system.Menu;
import steed.ext.domain.user.User;
import steed.hibernatemaster.domain.BaseDatabaseDomain;
import steed.hibernatemaster.util.DaoUtil;
import steed.util.base.BaseUtil;
import steed.util.base.PathUtil;

public class ExportDataBase {
	
	public static void main(String[] args){
		new UtilsUtil().initUtils();
		List<BaseDatabaseDomain> list = new ArrayList<>();
		list.addAll(DaoUtil.listAllObj(Config.class));
		Menu where = new Menu();
		where.setShow(true);
		list.addAll(DaoUtil.listAllObj(where));
		
		User user = DaoUtil.get(User.class, "admin");
		user.setPassword(null);
		user.setRoleSet(new HashSet<>());
		list.add(user);
		
		ObjectOutputStream objectOutputStream = null;
		try {
			objectOutputStream = new ObjectOutputStream(new FileOutputStream(getSqlDataPath()));
			objectOutputStream.writeObject(list);
			objectOutputStream.flush();
			BaseUtil.getLogger().debug("数据导出成功!");
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
				}
			}
		}
	}

	private static String getSqlDataPath() {
		String praseRelativePath = PathUtil.praseRelativePath("initsqldata.bin");
		BaseUtil.getLogger().debug("数据导出路径--->{}",praseRelativePath);
		return praseRelativePath;
	}
}
